package com.shuaiche.yeguan.ui.upload;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Dynamic;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableType;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.shuaiche.yeguan.R;
import com.shuaiche.yeguan.SCApplication;
import com.shuaiche.yeguan.config.SCAppConfig;
import com.shuaiche.yeguan.model.SCCarPicsModel;

import java.util.ArrayList;
import java.util.List;

public class UploadCarPic extends AppCompatActivity {

    private TabLayout tabLayout;
    private ViewPager2 viewPager;
    private Button btnConfirm;
    private ImageButton btnBack;
    private UploadPagerAdapter pagerAdapter;

    private int selectTab = 0;

    private ArrayList<String> carPicsList = new ArrayList<>();
    private ArrayList<SCCarPicsModel> certPicsList = new ArrayList<>();
    private ArrayList<String> otherPicsList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Window window = getWindow();
//        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//        window.setStatusBarColor(ContextCompat.getColor(this, R.color.white));

        window.getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        );

        setContentView(R.layout.activity_upload_car_pic);



        initViews();
        setupViewPager();
        setupListeners();
    }

    private void initViews() {
        tabLayout = findViewById(R.id.tab_layout);
        viewPager = findViewById(R.id.view_pager);
        btnConfirm = findViewById(R.id.btn_confirm);
        btnBack = findViewById(R.id.btn_back);

        selectTab = getIntent().getIntExtra("selectTab",0);
        List<SCCarPicsModel> carPicsModels = (List<SCCarPicsModel>) getIntent().getSerializableExtra("carPics");

        initCarPics(carPicsModels);
    }

    private void initCarPics(List<SCCarPicsModel> carPicsModels) {
        if (carPicsModels.isEmpty()){
            return;
        }
        for (SCCarPicsModel picModel : carPicsModels) {
            if (SCAppConfig.CAR_PIC.equals(picModel.getTitle())){//车辆图片
                carPicsList.add(picModel.getPicUrl());
            }else if (SCAppConfig.CERT_PIC.equals(picModel.getTitle())){
                certPicsList.add(picModel);
            }else if (SCAppConfig.OTHER_PIC.equals(picModel.getTitle())){
                otherPicsList.add(picModel.getPicUrl());
            }
        }
    }

    private void setupViewPager() {
        pagerAdapter = new UploadPagerAdapter(this,carPicsList,certPicsList,otherPicsList);
        viewPager.setAdapter(pagerAdapter);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                if (view != null) {
                    TextView tv_title = view.findViewById(R.id.tv_title);
                    TextView tv_line = view.findViewById(R.id.tv_line);
                    tv_title.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    tv_title.setTextColor(Color.parseColor("#333333"));
                    tv_line.setVisibility(View.VISIBLE);
                }
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                if (view != null) {
                    TextView tv_title = view.findViewById(R.id.tv_title);
                    TextView tv_line = view.findViewById(R.id.tv_line);
                    tv_title.setTextColor(Color.parseColor("#999999"));
                    tv_title.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    tv_line.setVisibility(View.INVISIBLE);
                }

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        new TabLayoutMediator(tabLayout, viewPager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                View customView = LayoutInflater.from(UploadCarPic.this).inflate(R.layout.custom_tab, null);
                TextView tvTitle = customView.findViewById(R.id.tv_title);

                if (position == 0){
                    tvTitle.setText("车辆图片");
                }else if (position == 1){
                    tvTitle.setText("证件资料");
                }else if (position == 2){
                    tvTitle.setText("其他照片");
                }
                tab.setCustomView(customView);
            }
        }).attach();
        viewPager.setOffscreenPageLimit(3);
        viewPager.setCurrentItem(selectTab);
    }

    private void setupListeners() {
        btnBack.setOnClickListener(v -> finish());

        btnConfirm.setOnClickListener(v -> {

            List<SCCarPicsModel>  uploadList = new ArrayList<>();
            uploadList.addAll(pagerAdapter.getCarPictureFragment().getCarPictures());
            uploadList.addAll(pagerAdapter.getDocumentFragment().getCertPics());
            uploadList.addAll(pagerAdapter.getOtherPictureFragment().getOtherPics());

            ReactContext reactContext = SCApplication.getApplication().getReactNativeHost().getReactInstanceManager().getCurrentReactContext();
            if (reactContext == null) {
                return;
            }

            WritableMap event = Arguments.createMap();

            event.putString("selectCarPicResult", JSON.toJSONString(uploadList));
            reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit("selectCarPicResult",event);

            finish();

        });
    }
}